package com.cloud.freeflow.api.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.freeflow.api.entity.ProcActDef;
import com.cloud.freeflow.api.entity.ProcActFilter;
import com.cloud.freeflow.api.entity.ProcPart;
import com.cloud.freeflow.api.mapper.ProcActDefMapper;
import com.cloud.freeflow.api.mapper.ProcActFilterMapper;
import com.cloud.freeflow.api.service.ProcFilterService;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class ProcFilterServiceImpl implements ProcFilterService {

	@Autowired
	private ProcActFilterMapper procActFilterMapper;

	@Autowired
	private ProcActDefMapper procActDefMapper;

	@Override
	public List<ProcPart> listActFilters(Long actDefId) {
		ProcActDef procActDef = procActDefMapper.selectByPrimaryKey(actDefId);
		return procActFilterMapper.listActFilters(procActDef.getProcDefId(), actDefId);
	}

	@Override
	public List<ProcActFilter> listActFilter(Long actDefId) {
		Example exp = new Example(ProcActFilter.class);
		Example.Criteria criteria = exp.createCriteria();
		criteria.andEqualTo("actDefId", actDefId);
		return procActFilterMapper.selectByExample(exp);
	}

	@Override
	public ProcActFilter saveActFilter(ProcActFilter filter) {
		procActFilterMapper.deleteByPrimaryKey(filter);
		if (!StringUtils.isEmpty(filter.getFilterSql()))
			procActFilterMapper.insertSelective(filter);
		return filter;
	}

	@Override
	public ProcActFilter loadActPartFilter(Long actDefId, Long partId) {
		ProcActFilter filter = new ProcActFilter();
		filter.setActDefId(actDefId);
		filter.setPartId(partId);
		return procActFilterMapper.selectByPrimaryKey(filter);
	}

	@Override
	public void deleteActPartFilter(ProcActFilter filter) {
		procActFilterMapper.deleteByPrimaryKey(filter);
	}

}
